
import { Component, Input } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';
import { Route, ActivatedRoute, Router } from '@angular/router';
import { CompanyService } from '../../company.service';

/**
 * Company header main component
 *
 * Author: Sorin Spiru
 * Date: 13.02.2018
 */
@Component({
    selector: 'company-menu',
    templateUrl: 'companyMenu.component.html',
    styleUrls: ['./companyMenu.component.scss']
})
export class CompanyMenuComponent {
    private sub: Subscription;

    public paths: any = {};

    public current: string;

    @Input() items: Route[];

    constructor(
        private route: ActivatedRoute,
        private router: Router,
        private companyService: CompanyService) {

    }

    ngOnInit() {
        this.sub = this.companyService.menuItems.subscribe(data => {
            //this.collapse = true;
            let subMenus = [];
            data.forEach(element => {
                if (element.data && element.data.showAsMenu == true && element.data.enabled == true && element.data.menuItems)
                    subMenus = subMenus.concat(element.data.menuItems);
            });
            this.paths[this.route.firstChild.routeConfig.path] = subMenus;
            this.current = this.route.firstChild.routeConfig.path;
        });
    }

    ngOnDestroy() {
        this.sub.unsubscribe();
    }

    menuClick(path) {
        this.current = path;
    }
}